import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { IView } from "../../mvc/interfaces/IView";
import { IntegralExchangeModule } from "../../../modules/IntegralExchangeModule";
import { WindowEvent } from "../../component/window/WindowEvent";
import { EPrictUnit } from "../../../Message/Public/EPrictUnit";
import { InExShopTabInfo } from "../view/IntegralExchangeShop/info/InExShopTabInfo";
import { EActiveType } from "../../../Message/Public/EActiveType";
import { IntegralShopItemNewConfig } from "../resource/IntegralShopItemNewConfig";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { SIntegralExchangeRecords } from "../../../Message/Public/SIntegralExchangeRecords";
import { SMoney } from "../../../Message/Game/SMoney";
import { TIntegralShopItemNew } from "../../../Message/Db/Tables/TIntegralShopItemNew";
import { ItemData } from "../resource/info/ItemData";
import { CareerUtil } from "../view/common/util/CareerUtil";
import { IIntegralExchangeModule } from "../../../modules/interfaces/IIntegralExchangeModule";
type int = number;
//class IntegralExchangeController
    
    export  class IntegralExchangeController extends Controller
    {
        constructor()
        {
            
            super();Dispatcher.addEventListener(EventName.IntegralExchangeOpen, this.onIntegralExchangeOpenHandler);
            Dispatcher.addEventListener(EventName.IntegralExchangeGetRecord, this.onIntegralExchangeGetRecordHandler);
            Dispatcher.addEventListener(EventName.IntegralExchangeBuyItem, this.onIntegralExchangeBuyItemHandler);
            return;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            NetDispatcher.addCmdListener(ServerCommand.IntegralExchangeRecordsCommand, this.onIntegralExchangeRecordsCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.onMoneyUpdateHandler);
            return;
        }

        public /* override */ get view(): IView
        {
            if (!this._integralModule) 
            {
                this._integralModule = new IntegralExchangeModule();
                this._integralModule.updateMoney(cache.role.money);
                this._integralModule.addEventListener(WindowEvent.SHOW, this.onAddedToStageHandler);
            }
            return this._integralModule;
        }

        /* internal  */onAddedToStageHandler(arg1: Object /* flash.events.Event */): void
        {
            var loc1=0;
            this._tabDataArray = cache.integralExchange.getIntegralTabDataArray(EPrictUnit._EPriceUnitIntegralOne, EPrictUnit._EPriceUnitIntegralTwo, EPrictUnit._EPriceUnitIntegralThree);
            this._integralModule.updateTabData(this._tabDataArray);
            var loc2=this._tabDataArray[0] as InExShopTabInfo;
            if (loc2) 
            {
                loc1 = loc2.shopId;
            }
            var loc3=cache.active.getActiveStateByCode(EActiveType._EActiveTypeNewIntegralShopItem);
            var loc4=IntegralShopItemNewConfig.instance.getItemsByShopId(loc1, loc3);
            this._integralModule.updateByIntegralShopItems(loc4);
            Dispatcher.dispatchEvent(new DataEvent(EventName.IntegralExchangeGetRecord, loc1));
            return;
        }

        /* internal  */onIntegralExchangeOpenHandler(arg1: DataEvent): void
        {
            var loc1=0;
            if (this.view.isHide) 
            {
                this.view.show();
            }
            else 
            {
                this.view.hide();
            }
            if (!(arg1.data == null) && this._integralModule) 
            {
                loc1 = arg1.data as int;
                this._integralModule.updateShopByIndex(loc1);
            }
            return;
        }

        /* internal  */onIntegralExchangeGetRecordHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.shopProxy.getIntegralExchangeRecords(loc1);
            return;
        }

        /* internal  */onIntegralExchangeBuyItemHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["shopId"] as int;
            var loc2=arg1.data["itemCode"] as int;
            var loc3=arg1.data["amount"] as int;
            GameProxy.shopProxy.integralExchange(loc1, loc2, loc3);
            return;
        }

        /* internal  */onIntegralExchangeRecordsCommandHandler(arg1: SIntegralExchangeRecords): void
        {
            if (this._integralModule && !this._integralModule.isHide) 
            {
                this._integralModule.updateByRecords(arg1);
            }
            return;
        }

        /* internal  */onMoneyUpdateHandler(arg1: SMoney): void
        {
            if (this._integralModule && !this._integralModule.isHide) 
            {
                this._integralModule.updateMoney(arg1);
            }
            return;
        }

        /* internal  */careerFiler(arg1: TIntegralShopItemNew, arg2: int, arg3: Array<any>): Boolean
        {
            var loc1;
            var loc2=(loc1 = new ItemData(arg1.itemCode)).itemInfo.item.career;
            return CareerUtil.isCareerSuit(loc2, cache.role.entityInfo.career);
        }

        private /* var */_integralModule: IIntegralExchangeModule;

        private /* var */_tabDataArray: Array<any>;
    }
